<?php
/**
 * Created by PhpStorm.
 * User: xiaomage
 * Date: 2020/12/15
 * Time: 10:34
 */

namespace app\system\controller;

use common\ApiError;
use common\ApiReturn;
use component\system\system_auth\SystemAuth;
use component\system\system_auth\SystemAuthValidate;
use component\system\system_role\SystemRole;
use component\system\system_user_auth\SystemUserAuth;
use component\system\system_user_menu\SystemUserMenu;
use component\system\system_user_role\SystemUserRole;

class Auth extends System
{
    /**
     * @return \think\Response
     */
    public function module()
    {
        $title = request()->param('title');

        $result = SystemAuth::instance()->postModule($title);

        if ($result) {
            return ApiReturn::success();
        }

        return ApiReturn::error();
    }

    /**
     * 添加restful权限
     * @return \think\Response
     */
    public function restful()
    {
        $data = request()->only(['pid', 'title', 'desc', 'route']);
        $result = SystemAuth::instance()->postRestful($data);

        if ($result) {
            return ApiReturn::success(SystemAuth::instance()->first());
        }

        return ApiReturn::error();
    }

    /**
     * @return array|\think\Response
     */
    public function post()
    {
        $data = request()->only(['id', 'pid', 'title', 'path', 'desc']);

        if ($data['path']) {
            $record = SystemAuth::instance()->getByPath($data['path']);
            if ($record && $record['id'] != $data['id']) {
                return ApiReturn::error('请不要重复添加' . $data['title'] ?: '');
            }
        }

        $data['level'] = 3;

        $result = SystemAuth::instance()->setValidate(SystemAuthValidate::class)->save($data);
        if ($result) {
            return ApiReturn::success($result);
        }

        return ApiReturn::exception();
    }

    /**
     * @return \think\Response
     */
    public function get()
    {
        $id = request()->param('id');

        $params = [];
        if ($id) {
            $params['id'] = $id;
        }

        $result = SystemAuth::instance()->select($params);

        return ApiReturn::success($result);
    }

    /**
     * 绑定角色权限
     * @return \think\Response
     */
    public function setRoleAuth()
    {
        $role_id = request()->param('role_id');
        $auth_id = request()->param('auth_id');

        return ApiReturn::auto(SystemRole::instance()->setAuth($role_id, $auth_id));
    }

    /**
     * 获取角色授权列表
     * @return \think\Response
     */
    public function getRoleAuthList()
    {
        $role_id = request()->param('role_id');

        $result = SystemRole::instance()->getRoleAuthList($role_id);

        return ApiReturn::auto($result);
    }

    /**
     * 设置角色菜单
     * @return \think\Response
     */
    public function setRoleMenu()
    {
        $role_id = request()->param('role_id');
        $menu = request()->param('menu');

        return ApiReturn::auto(SystemRole::instance()->setMenu($role_id, $menu));
    }

    /**
     * 设置角色菜单
     * @return \think\Response
     */
    public function getRoleMenuList()
    {
        $role_id = request()->param('role_id');

        return ApiReturn::auto(SystemRole::instance()->getRoleMenuList($role_id));
    }


    /**
     * 设置用户组、权限、菜单
     * @return \think\Response
     */
    public function setUserAuth()
    {
        $user_id = request()->param('user_id');
        $role_id = request()->param('role_id');
        $auth_id = request()->param('auth_id', []);
        $menu = request()->param('menu', []);
        $params = [];
        if ($role_id) {
            $params['role_id'] = $role_id;
        }

        if ($auth_id) {
            $params['auth_id'] = $auth_id;
        }

        if ($menu) {
            $params['menu'] = $menu;
        }

        return ApiReturn::auto(SystemUserAuth::instance()->setUserAuth($user_id, $params));
    }

    /**
     * 获取用户所属角色
     * @return \think\Response
     */
    public function getUserAuth()
    {
        $user_id = request()->param('user_id');

        return ApiReturn::auto(SystemUserAuth::instance()->getByUserId($user_id));
    }
}